#include "pch.h"
#include "badge.h"


JOUI::UIBadge::UIBadge(UIBase* hParent, INT x, INT y, INT width, INT height, INT nID)
{
	UIBase* Parent = nullptr;
	if (hParent->m_UIView)
	{
		auto Bind = (UIControl*)hParent->m_UIView;
		Bind->GetRect(p_data.bind);
		Parent = (UIBase*)Bind->GetParent();
		p_data.BindUIView = hParent->m_UIView;
	}
	else
	{
		auto Bind = (UIWnd*)hParent->m_UIWindow;
		Bind->GetRect(p_data.bind);
		Parent = (UIWnd*)Bind->m_UIWindow;
	}
	InitSubControl(Parent, x, y, width, height, L"form-badge", NULL, 0, 0, nID, Middle | Center | SingleLine);
	SetFontFromFamily(NULL, 11);
}

void JOUI::UIBadge::SetColor(UIColor dwCrBkg, UIColor dwCrText)
{
	p_data.Color[0] = dwCrBkg;
	p_data.Color[1] = dwCrText;
}

void JOUI::UIBadge::SetBadgeType(badge_type type)
{
	p_data.type = type;
	if (p_data.type == dot) {
		Move(p_data.bind.right - 4, p_data.bind.top - 3, 10, 10, TRUE);
	}
}

void JOUI::UIBadge::EnableShowZero(BOOL enable)
{
	p_data.bszero = enable;
}

void JOUI::UIBadge::SetMaxNumber(INT max)
{
	p_data.max = max;
	SetNumber(p_data.number);
}

void JOUI::UIBadge::SetNumber(INT number)
{
	p_data.number = number;

	std::wstring tit = L"";
	if (p_data.max != 0 && p_data.number < p_data.max) tit = std::to_wstring(p_data.number).append(L"+").c_str();
	else tit = std::to_wstring(p_data.number).c_str();

	float tw = 0, th = 0;
	UICanvas::CalcTextSize((UIFont*)GetFont(), tit.c_str(), -1, m_data.dwTextFormat, 9999, 9999, &tw, &th);
	if (p_data.number > p_data.max)
		tw += 6;
	Move(p_data.bind.right - tw / 2 - 5, p_data.bind.top - th / 2 + 4, tw + 5, th + 4, TRUE);

}


LRESULT JOUI::UIBadge::OnMsgProc(HWND hWnd, INT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_PAINT) {
		badge_paint(hWnd);
	}
	else if (uMsg == WM_SETTEXT)
	{
		float tw = 0, th = 0;
		UICanvas::CalcTextSize((UIFont*)GetFont(), m_data.pstrTitle, -1, m_data.dwTextFormat, 9999, 9999, &tw, &th);
		Move(p_data.bind.right - tw / 2 - 5, p_data.bind.top - th / 2 + 4, tw + 6, th + 4, TRUE);
	}
	return S_OK;
}

void JOUI::UIBadge::badge_paint(HWND hWnd)
{
	ps_context ps{};

	if (BeginPaint(ps))
	{
		UIBrush *br = new UIBrush(p_data.Color[0]);
		if (p_data.type == dot) {
			ps.hCanvas->DrawPoint(br, 3, 3, 5, TRUE);
		}
		else if (p_data.type == number) {
			if (p_data.number != 0 || p_data.bszero) {
				std::wstring tit = L"";
				if (p_data.max == 0 || p_data.number <= p_data.max) tit = std::to_wstring(p_data.number).c_str();
				else tit = std::to_wstring(p_data.max).append(L"+").c_str();

				ps.hCanvas->FillRoundRect(br, 0, 0, ps.uWidth, ps.uHeight, (float)min(ps.uHeight, ps.uWidth) / 2);
				ps.hCanvas->DrawTextByColor(ps.hFont, tit.c_str(), -1, ps.dwTextFormat, 1, 0, ps.uWidth - 2, ps.uHeight - 2, p_data.Color[1]);
			}
		}
		else if (p_data.type == content) {
			if (m_data.pstrTitle || p_data.bszero) {
				ps.hCanvas->FillRoundRect(br, 0, 0, ps.uWidth, ps.uHeight, (float)min(ps.uHeight, ps.uWidth) / 2);
				ps.hCanvas->DrawTextByColor(ps.hFont, m_data.pstrTitle, -1, ps.dwTextFormat, 1, 0, ps.uWidth - 2, ps.uHeight - 2, p_data.Color[1]);
			}
		}
	
		delete br;


		EndPaint();
	}
}